﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace ToDoList.Entity.Models
{
    public partial class ToDoListDataContext : DbContext
    {
        public virtual DbSet<ItemsData> ItemsData { get; set; }
        public virtual DbSet<ListData> ListData { get; set; }
        public virtual DbSet<UsersData> UsersData { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
            optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-RQSBPC2; Initial Catalog = ToDoListData; User ID = sa; Password = woaiwoziji201011;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ItemsData>(entity =>
            {
                entity.HasKey(e => e.EventId)
                    .HasName("PK_EventData");

                entity.Property(e => e.CreationTime).HasColumnType("datetime");

                entity.Property(e => e.EvenName)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.OverdueEvent).HasColumnType("datetime");

                entity.Property(e => e.Remarks)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.HasOne(d => d.List)
                    .WithMany(p => p.ItemsData)
                    .HasForeignKey(d => d.ListId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_EventData_ListData");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.ItemsData)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_EventData_UsersData");
            });

            modelBuilder.Entity<ListData>(entity =>
            {
                entity.HasKey(e => e.ListId)
                    .HasName("PK_ListData");

                entity.Property(e => e.CreationTime).HasColumnType("datetime");

                entity.Property(e => e.ListName)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.HasOne(d => d.User)
                    .WithMany(p => p.ListData)
                    .HasForeignKey(d => d.UserId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_ListData_UsersData");
            });

            modelBuilder.Entity<UsersData>(entity =>
            {
                entity.HasKey(e => e.UserId)
                    .HasName("PK_UsersData");

                entity.Property(e => e.Password)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.UserName)
                    .IsRequired()
                    .HasMaxLength(50);
            });
        }
    }
}